export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

export function deepCopy(o) {
  if (o instanceof Array) {
    var n = [];
    for (var i = 0; i < o.length; ++i) {
      n[i] = deepCopy(o[i]);
    }
    return n;
  } else if (o instanceof Function) {
    var n = new Function("return " + o.toString())();
    return n
  } else if (o instanceof Object) {
    var n = {}
    for (var i in o) {
      n[i] = deepCopy(o[i]);
    }
    return n;
  } else {
    return o;
  }
}

export function parsePageOpts(_pageOpts) {
  const pageOptsStr = JSON.stringify(_pageOpts)
  const pageOpts = JSON.parse(pageOptsStr)
  const sortArr = []
  for (const item in pageOpts.sort) {
    sortArr.push({
      key: item,
      order: pageOpts.sort[item]
    })
  }

  const status = pageOpts.status.join(",")
  const sort = sortArr
    .map(({
      key,
      order
    }) => order === "descending" ? `-${key}` : `${key}`)
    .join(",")
  return { ...pageOpts,
    status,
    sort
  }


}